<?php
	require_once('start_session.php');
	if ((!isset($_POST['location']) && !isset($_POST['latitude']) && !isset($_POST['longitude']))
				|| !isset($_POST['ip']) || !isset($_POST['connect_time']) || !isset($_POST['location_type'])) {
		header("Location: index.php");
		exit;
	}
	
	require_once('db_connect.php');
	
	$ip = $_POST['ip'];
	$connect_time = $_POST['connect_time'];
	$locType = $_POST['location_type'];
	
	$location = null;
	$latitude = null;
	$longitude = null;
	if ($locType == 'indoor')
		$location = $_POST['location'];
	else {
		$latitude = $_POST['latitude'];
		$longitude = $_POST['longitude'];
	}

	$_SESSION['ip'] = $ip;
	$_SESSION['connect_time'] = $connect_time;
	$_SESSION['location'] = $location;
	$_SESSION['latitude'] = $latitude;
	$_SESSION['longitude'] = $longitude;

	
	$query = "insert into connect_log values('$connect_time', '$ip')";
	mysqli_query($conn, $query)
		or die("connect log에 $ip, $connect_time 삽입쿼리 실패");
		
	
	$query = "select * from session where ip='$ip'";
	$len=mysqli_num_rows(mysqli_query($conn, $query));
	
	if($len > 0)
	{
		echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"><script>window.alert('해당아이피는 접속중입니다')</script>";
		echo "<script>history.go(-1)</script>";
		return;
	}	
	$query = "insert into session (ip,last_time) values('$ip', '$connect_time')";
	mysqli_query($conn, $query)
		or die("session에 $ip, $connect_time 삽입쿼리 실패");	

		
	header("Location: main.php");
?>